//
//  LCRankingListViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/11.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCRankingListViewController.h"
#import "LCMineService.h"
#import "LCRankingModel.h"
#import "LCRankTableViewCell.h"

@interface LCRankingListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, assign) BOOL canScroll;

@property (assign, nonatomic)NSInteger page;

@property (strong, nonatomic)NSMutableArray * dataArr;

@end

@implementation LCRankingListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self setTableViewRefresh];
    [self getRankData];
}

-(void)configUI {
    self.page = 1;
    self.canScroll = NO;
    [LCNotiCenter addObserver:self selector:@selector(rankListControllerGoTop:) name:LCRankListControllerGoTopNoti object:nil];
    [LCNotiCenter addObserver:self selector:@selector(rankListControllerLeaveTop:) name:LCRankPageControllerLeaveTopNoti object:nil];
}

-(void)rankListControllerGoTop:(NSNotification *)noti {
    self.canScroll = YES;
}

-(void)rankListControllerLeaveTop:(NSNotification *)noti {
    self.canScroll = NO;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.canScroll) {
        CGFloat offsetY = scrollView.contentOffset.y;
        if (offsetY <= 0) {
            [self.tableView setContentOffset:CGPointZero];
            [[NSNotificationCenter defaultCenter] postNotificationName:LCRankPageControllerLeaveTopNoti object:@(YES)];
        }
    } else {
        [self.tableView setContentOffset:CGPointZero];
    }
}

-(void)setTableViewRefresh {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getRankData];
    }];
    
    self.tableView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

-(void)getRankData {
    [LCMineService rankingListWithPage:self.page Block:^(BOOL isSuccess, NSArray<LCRankingModel *> *list, NSInteger total) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCRankTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCRankTableViewCell"];
    cell.ranking = self.dataArr[indexPath.row];
    if (indexPath.row <= 1) {
        cell.rankNoImgV.hidden = NO;
        cell.rankNumLb.hidden = YES;
        cell.rankNoImgV.image = [UIImage imageNamed:kFORMAT(@"lc_mine_tool_rank_%ld",indexPath.row)];
    } else {
        cell.rankNoImgV.hidden = YES;
        cell.rankNumLb.hidden = NO;
    }
    return cell;
}


@end
